package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.crm.entity.MmrzT;
import cn.e4j.modular.crm.service.MmrzTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 知识库维护作业
 *
 * @author yuan_rz
 * @Date 2019-12-24
 */
@Controller
@RequestMapping("/crm/gwai820")
public class Gwai820Controller extends BaseController {

    @Autowired
    private MmrzTService mmrzTService;
    

    /**
     * 跳转页面
     */
    @RequiresPermissions("crm:gwai820:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai820.html";
    }

    /**
     * 查询信息列表
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gwai820:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrzTService.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加信息
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwai820:init")
    @ResponseBody
    public ResponseData addData(MmrzT mmrzT) {
        mmrzTService.addData(mmrzT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwai820:init")
    @ResponseBody
    public ResponseData editData(MmrzT mmrzT) {
        mmrzTService.editData(mmrzT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("crm:gwai820:init")
    @ResponseBody
    public ResponseData deleteData(MmrzT mmrzT) {
        mmrzTService.deleteData(mmrzT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai820:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrzT> MmrzTList = inDto.getReqList(MmrzT.class);
        mmrzTService.deleteDataBatch(MmrzTList);
        return SUCCESS_TIP;
    }

    /**
     * 导入数据
     */
    @RequestMapping(value = "/importListData", method = RequestMethod.POST)
    @RequiresPermissions("crm:gwai820:init")
    @ResponseBody
    public ResponseData importListData(MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//表格标题行数,默认0
        params.setHeadRows(1);//表头行数,默认1
        params.setStartRows(0);//字段真正值和列标题之间的距离 默认0
        try {
            List<MmrzT> list = ExcelImportUtil.importExcel(file.getInputStream(), MmrzT.class, params);
            mmrzTService.addDataBatch(list);
        } catch (Exception e) {
            e.printStackTrace();//打印错误
            return ResponseData.error("upload error");
        }
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/exportListData", method = RequestMethod.GET)
    @RequiresPermissions("crm:gwai820:init")
    public void exportListData(@DtoParam DtoWapper dtoWapper, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        Dto inDto = dtoWapper.getDto();
        List<MmrzT> list = mmrzTService.exportListData(inDto);
        ExportParams params = new ExportParams(null, "知识库", ExcelType.XSSF);
        map.put(NormalExcelConstants.FILE_NAME, "知识库");
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, MmrzT.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
